package com.hoperun.controller.common;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import com.hoperun.utils.CustomTimestampEditor;
import com.hoperun.utils.StringEscapeEditor;

/**
 * Copyright: Copyright (c) 2014 CNPCMALL <br/>
 * ClassName: GlobalController.java
 * 
 * @Description: 该类的功能描述
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2014-8-14 下午1:23:04
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2014-8-14
 *        fuyu v1.0.0 修改原因
 */
@ControllerAdvice
public class GlobalController
{
	
	@InitBinder
	public void initBinder(WebDataBinder binder)
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		SimpleDateFormat datetimeFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		datetimeFormat.setLenient(false);
		// 自动转换日期类型的字段格式
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
		binder.registerCustomEditor(java.sql.Timestamp.class,
				new CustomTimestampEditor(datetimeFormat, true));
		// 防止XSS攻击
		binder.registerCustomEditor(String.class, new StringEscapeEditor(false,
				false, true));
	}
}
